package net.lahuobao.export.service.impl;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.lahuobao.export.datasoruce.annotation.ExportField;
import net.lahuobao.export.datasoruce.entity.Paybill;
import net.lahuobao.export.service.comm.IHelp;
import net.lahuobao.export.service.comm.ITranfGen;
import net.lahuobao.export.service.tranf.INullTranf;
/**
 * 生成数据
 * @author Administrator
 *
 */
@Service("helpService")
public class HelperImpl implements IHelp {
	@Autowired
	private ITranfGen tranfGenService;
	@Autowired
	private INullTranf nullTranfService;
	/**
	 * 生成表头
	 */
	@Override
	public <T> String CreateTableHeader(Object c) throws InstantiationException, IllegalAccessException {		
		//获得该bean的属性
        Field[] fields = c.getClass().getDeclaredFields();
        Arrays.sort(fields,new SortFeild());
        String header="";
        for(Field field:fields){
            if(field.getAnnotation(ExportField.class).isShow()!=0){
            	header+=field.getAnnotation(ExportField.class).title()+"	";
            }
          }
		return header;
	}
	/**
	 * 生成一行数据
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	@Override
	public  String CreateTableItem(Object c) throws InstantiationException, IllegalAccessException {
		//获得该bean的属性
        Field[] fields = c.getClass().getDeclaredFields();
        Arrays.sort(fields,new SortFeild());
        String item="";
        for(Field field:fields){
            if(field.getAnnotation(ExportField.class).isShow()!=0){
            	field.setAccessible(true);//设置允许访问  
                PropertyDescriptor propDesc=null;
				try {
					propDesc = new PropertyDescriptor(field.getName(),c.getClass());
				} catch (IntrospectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                Method methodGet=propDesc.getReadMethod();
                Object d="";
				try {
					//是否翻译
	            	if(field.getAnnotation(ExportField.class).isTranf()==1){
	            		d=tranfGenService.tranf(c,field,field.getAnnotation(ExportField.class).tranfType());
	            	}else{
						d = methodGet.invoke(c);
	            	}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	item+=nullTranfService.nullTranf(d)+"	";
            }
            
        }
		return item;
	}
	
	
	class SortFeild implements Comparator<Field>{
	    //重写该接口的compare()使其按照学号由小到大排序（前者减去后者）
	    @Override
	    public int compare(Field f1, Field f2) {
	    	int a=f1.getAnnotation(ExportField.class).sort();
	    	int b=f2.getAnnotation(ExportField.class).sort();
	        return a-b;
	         
	    }
	     
	}


	@Override
	public <T> String[] CreateTableHeaderArray(Object o) throws InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		
		//获得该bean的属性
        Field[] fields = o.getClass().getDeclaredFields();
        Arrays.sort(fields,new SortFeild());
        
        java.util.ArrayList<String> refArray = new java.util.ArrayList<String>();
        
        for(Field field:fields){
            if(field.getAnnotation(ExportField.class).isShow()!=0){
            	refArray.add(field.getAnnotation(ExportField.class).title());
            }
          }
				
        String[] array =new String[refArray.size()];;
        refArray.toArray(array);
		return array;
	}
	
	
	@Override
	public <T> String[] CreateTableItemArray(Object c) throws InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		
		
		//获得该bean的属性
        Field[] fields = c.getClass().getDeclaredFields();
        Arrays.sort(fields,new SortFeild());
        //String item="";
        java.util.ArrayList<String> refArray = new java.util.ArrayList<String>();
        for(Field field:fields){
            if(field.getAnnotation(ExportField.class).isShow()!=0){
            	field.setAccessible(true);//设置允许访问  
                PropertyDescriptor propDesc=null;
				try {
					propDesc = new PropertyDescriptor(field.getName(),c.getClass());
				} catch (IntrospectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                Method methodGet=propDesc.getReadMethod();
                Object d="";
				try {
					//是否翻译
	            	if(field.getAnnotation(ExportField.class).isTranf()==1){
	            		d=tranfGenService.tranf(c,field,field.getAnnotation(ExportField.class).tranfType());
	            	}else{
						d = methodGet.invoke(c);
	            	}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				refArray.add(nullTranfService.nullTranf(d).toString());
            	//item+=nullTranfService.nullTranf(d)+"	";
            }
            
        }
        
        String[] array =new String[refArray.size()];;
        refArray.toArray(array);
		return array;
		
		
	}
}
